package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Link;
import com.itheima.ydd.service.LinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 友情链接管理
 */
@Slf4j
@RestController
@RequestMapping("ydd_link")
public class LinkController {
    @Autowired
    private LinkService linkService;

    /**
     * 返回链接详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("返回链接详情id:{}", id);
        Link link = linkService.getById(id);
        return ResultData.ok("", link);
    }

    /**
     * 链接列表-分页
     *
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResultData list(String type, Integer page, Integer limit) {
        log.info("链接列表-分页type:{},页码page:{},每页数量limit:{}", type, page, limit);
        HashMap<String, Object> map = linkService.getPageHashMap(type, page, limit);
        return ResultData.ok("", map);
    }

    /**
     * 链接删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData del(Integer id) {
        log.info("链接删除id:{}", id);
        linkService.removeById(id);
        return ResultData.ok("", "链接删除成功");
    }

    /**
     * 链接修改
     *
     * @param link
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Link link) {
        log.info("分类修改link:{}", link);
        linkService.updateById(link);
        return ResultData.ok("", "链接修改成功");
    }

    /**
     * 链接添加
     *
     * @param link
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Link link) {
        log.info("链接添加Link:{}", link);
        link.setDefault_data("1");
        linkService.save(link);
        return ResultData.ok("", "链接添加成功");
    }
}
